/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <fstream>
#include <sstream>
#include <iomanip>
#include <cassert>

#include <time.h>
#include <stdlib.h>

#include <QMessageBox>
#include <QFont>

#include "WordManager.h"
#include "Logger.h"

WordManager::WordManager():
	mCurWordID(-1),
	mCurWordIndex(-1),
	mLastWordID(-1)
{
	loadWordSet();
	srand((int)time(0));
}

WordManager::~WordManager()
{
	finishCurTask();
}

void WordManager::addWord(int id)
{
	//LOG<<"WordManager::addWord"<<endl;
	mWordsVector.push_back(id);
	mWordsMap.insert(make_pair(id, new Word(id)));
}

void WordManager::getBasicInfoByResumeList()
{
	/*if(mResumeWords.empty())
	{
		LOG<<"WordManager::getBasicInfoByResumeList-->empty mResumeWords"<<endl;
		QCoreApplication::quit();
	}

	QString queryCommand;
	queryCommand.clear();
	vector<QString>requirements;
	requirements.push_back("id");
	requirements.push_back("word");
	requirements.push_back("phonetic_symbol");
	requirements.push_back(DictNames::getDataBaseColumnName(DictNames::GRERedBible));

	queryCommand.append("select ");
	for (int i=0; i<requirements.size()-1; i++)
		queryCommand.append(requirements[i]).append(", ");
	queryCommand.append(requirements[requirements.size()-1]);
	queryCommand.append(" from words where ");
	for (int i=0; i<mResumeWords.size(); i++)
	{
		int id = mResumeWords[i];
		queryCommand.append(QString("id = %1").arg(id));
		if(i < mResumeWords.size()-1)
			queryCommand.append(" or ");
	}
	queryCommand.append(";");
	DB.execQuery(queryCommand, QueryT_Read, DB.curUsr);

	for (int i=0; i<DB.getRowCount(); i++)
	{
		int id = DB.getTableValue(i, "id").toInt();
		QString word = DB.getTableValue(i, "word");
		QString phonetic_symbol = DB.getTableValue(i, "phonetic_symbol");
		QString simple_explanation = DB.getTableValue(i, "simple_explanation");

		addWord(id);
		mWordsMap[id].setID(id);
		mWordsMap[id].setword(word);
		mWordsMap[id].setSoundMark(phonetic_symbol);
		mWordsMap[id].setRedBibleExp(simple_explanation);//essential
	}
	*/
}

void WordManager::getBasicInfoByActiveState()
{
	QString queryCommand;

	queryCommand.clear();
	vector<QString>requirements;

	requirements.push_back("words.id");
	requirements.push_back("words.word");
	requirements.push_back("words.phonetic_symbol");
	requirements.push_back("words.simple_explanation");
	requirements.push_back("words.list");

	queryCommand.append("select ");
	for (int i=0; i<requirements.size()-1; i++)
		queryCommand.append(requirements[i]).append(", ");
	queryCommand.append(requirements[requirements.size()-1]);
	queryCommand.append(" from words,test_records where words.id = test_records.id and  test_records.is_active = 1;");

	DB.execQuery(queryCommand, QueryT_Read, DB.curUsr);
	for (int i=0; i<DB.getRowCount(); i++)
	{
		int id = DB.getTableValue(i, "id").toInt();
		QString word = DB.getTableValue(i, "word");
		QString phonetic_symbol = DB.getTableValue(i, "phonetic_symbol");
		QString simple_explanation = DB.getTableValue(i, "simple_explanation");
		int list = DB.getTableValue(i, "list").toInt();
		addWord(id);
		mWordsMap[id]->ID = id;
		mWordsMap[id]->word = word;
		mWordsMap[id]->phoneticSymbol = phonetic_symbol;
		mWordsMap[id]->simpleExplanation = simple_explanation;//essential
		mWordsMap[id]->listID = list;
	}
}

void WordManager::clearWordContainers()
{
	//LOG<<"WordManager::clearWordContainers"<<endl;
	mCurWordID = -1;
	mCurWordIndex = -1;

	for (	map<int, Word*>::iterator it = mWordsMap.begin();
			it != mWordsMap.end();
			it++)
		delete it->second;

	mWordsMap.clear();
	mWordsVector.clear();
}

void WordManager::finishCurTask()
{
	clearWordContainers();
}

void WordManager::browseWordByIndex(int index)
{
	if (index >= mWordsVector.size())
	{
		mCurWordIndex = mWordsVector.size()-1;
		mCurWordID = mWordsVector[mCurWordIndex];
	}
	else if(index < 0)
	{
		mCurWordIndex = 0;
		mCurWordID = mWordsVector[mCurWordIndex];
	}
	else
	{
		mCurWordIndex = index;
		mCurWordID = mWordsVector[mCurWordIndex];
	}
}

Word* WordManager::getCurWord()
{
	//LOG<<"WordManager::getCurWord"<<endl;
	map<int, Word*>::iterator word_it;
	word_it = mWordsMap.find(mCurWordID);
	if(word_it != mWordsMap.end())
	{
		//LOG<<"WordManager::getCurWord-->word is = "<<word_it->second.getID()<<endl;
		return word_it->second;
	}
	else
		return NULL;
}

Word* WordManager::getWord(int id)
{
	map<int, Word*>::iterator word_it;
	word_it = mWordsMap.find(id);
	if(word_it != mWordsMap.end())
		return word_it->second;
	else
		return NULL;
}

Word* WordManager::getWordByIndex(int index)
{
	int id = mWordsVector[index];
	return getWord(id);
}

int WordManager::random(int size)
{
	//srand((int)time(0));
	int i=0+(int)(float(size)*rand()/(RAND_MAX+1.0));
	return i;
}

void WordManager::loadWordSet()
{
	mWordSet.clear();

	SQLiteDB db;
	QString command;
	QString dbName = QString("new");
	QString dbFile = QString("./data/user/new.db");
	if (!db.openDB(dbFile, dbName))
	{
		LOG<<"Fail to open database"<<dbName.toUtf8().data()<<endl;
		return;
		//assert(0);
	}

	command = "select word from words;";
	db.execQuery(command, QueryT_Read, "new");
	for (int i=0; i<db.getRowCount(); i++)
		mWordSet.insert(db.getTableValue(i, "word"));
}

void WordManager::loadWordTransformation()
{
	mWordTransformation.clear();
	SQLiteDB db;
	QString command;
	QString dbName = QString("transformation.red_bible");
	QString dbFile = QString("./data/common/transformation.red_bible.db");
	if (!db.openDB(dbFile, dbName))
	{
		LOG<<"Fail to open database"<<endl;
		return;
		//assert(0);
	}

	command = "select key,trans from transformation;";
	db.execQuery(command, QueryT_Read, dbName);
	for (int i=0; i<db.getRowCount(); i++)
		mWordTransformation.insert(make_pair(db.getTableValue(i, "key"), db.getTableValue(i, "trans")));
}

